package com.zhuhjay.service.imchat.model.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.zhuhjay.common.base.constant.TimeConstant;
import com.zhuhjay.common.base.util.BeanBuilder;
import com.zhuhjay.service.imchat.model.entity.ChatMessage;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Min;
import java.io.Serializable;
import java.util.Date;

/**
 * @author ZhuHJay
 * @date 2023/7/13 11:03
 */
@Data
public class ChatMessageDto implements Serializable {

    private Integer userId;
    private String username;
    private String avatar;
    @Min(value = 1, message = "聊天室id必须大于0")
    private Integer roomId;
    @Length(min = 1, max = 1024, message = "消息内容长度必须在1-1024之间")
    private String content;
    private Long replyId;
    private Integer gapCount;
    @JsonFormat(pattern = TimeConstant.DATE_TIME_FORMAT_UTC, timezone = "GMT+8")
    private Date sendTime;

    public static ChatMessage toJavaBean(ChatMessageDto dto) {
        return BeanBuilder.builder(ChatMessage::new)
                .with(ChatMessage::setUserId, dto.userId)
                .with(ChatMessage::setRoomId, dto.roomId)
                .with(ChatMessage::setContent, dto.content)
                .with(ChatMessage::setReplyId, dto.replyId)
                .with(ChatMessage::setGapCount, dto.gapCount)
                .with(ChatMessage::setStatus, 1)
                .with(ChatMessage::setCreateTime, dto.sendTime)
                .with(ChatMessage::setUpdateTime, dto.sendTime)
                .build();
    }

}
